var express = require('express');
var router = express.Router();
var JPush = require("../lib/JPush.js");
var mongodb = require('mongodb');
var server = new mongo.Server('localhost', 27017, {
							socketOptions:{connectTimeoutMS: 500}, 
							auto_reconnect: true, 
							poolSize: 10
						});

var db = new mongodb.Db('cardb', server, {safe: true});


//test
router.get('/test', function(req, res){
	db.open(function(err, client) {
		if(err){
			console.log('Connection Failed');
		} else {
			console.log('connect db');
			db.createCollection('t_driver', {safe: true}, function(err, collection){
				if(err){
					console.log(err);
				} else{
					collection.find().toArray(function(err, docs){
						console.log('find');
						console.log(docs);
						res.jsonp(docs);
					});
				}
			});
		}
	});	
});



var driverAll = [];
var d_pAll = [];

var pushTerminal = function(appkey, masterSecret, message, type){
	var client = JPush.buildClient(appkey, masterSecret);

	//easy push
	client.push().setPlatform(JPush.ALL)
	    .setAudience(JPush.alias(type))
	    .setNotification(message, JPush.android('android alert', null, 1))
	    .send(function(err, res) {
	        if (err) {
	            console.log(err.message);
	        } else {
	            console.log('Sendno: ' + res.sendno);
	            console.log('Msg_id: ' + res.msg_id);
	        }
	    });
}

//passenger and driver's address
router.get('/toforeground', function(req, res){
	var address = [{"driver":driverAll},{"passenger":d_pAll}];
	res.jsonp(address);
});

//passenger
router.post('/getdriver', function(req, res){
	var passenger = req.body.username;
	var number = 0;
	for (var o in d_pAll){
		if(d_pAll[o].passenger == passenger){
			for(var d in driverAll){
				if(driverAll[d].driver == d_pAll[o].driver){
					res.jsonp(driverAll[d]);
					break;
				}
			}
			break;			
		} else{
			number++;
		}
	};

	if(number = d_pAll.length){
		res.jsonp(driverAll);
	}	
});

router.post('/postpassenger', function(req, res){
	var passenger = req.body.username;
	var lat = req.body.lat;
	var lon = req.body.lon
	var passadd = {"passenger":passenger, "lat":lat, "lon":lon, "driver":""};

	var number = 0;
	for (var o in d_pAll){
		if(d_pAll[o].passenger == passenger){
			d_pAll[o].lat = lat;
			d_pAll[o].lon = lon;
		} else {
			number++;
		}
	};

	if(number = d_pAll.length){
		passengerAll.push(passadd);
	}
	
	res.jsonp({status:'ok'});
});

router.post('/passengercall', function(req, res){
	var passenger = req.body.username;
	pushTerminal('8fe07689d84f644ac617973b', '8bedebfc1a7e50b9b79cfcac', passenger, 'driver');
	res.jsonp({status:'ok'});
});

router.post('/passengercancel', function(req, res){
	var passenger = req.body.username;
	var i = 0;
	for (var o in d_pAll){
		if(d_pAll[o].passenger == passenger){
			d_pAll.splice(i, 1);
			break;
		}
		i++;
	};

	pushTerminal('8fe07689d84f644ac617973b', '8bedebfc1a7e50b9b79cfcac', passenger, 'driver');
	res.jsonp({status:'ok'});
});



router.post('/getpassenger', function(req, res){
	var driver = req.body.username;
	for (var o in d_pAll){
		if(d_pAll[o].driver == driver){
			res.jsonp(d_pAll[o]);
		}
	};
});



router.post('/postdriver', function(req, res){
	var dirver = req.body.username;
	var lat = req.body.lat;
	var lon = req.body.lon;
	var driverAdd = {"driver":driver, "lat":lat, "lon":lon};

	var number = 0;
	for (var o in driverAll){
		if(driverAll[o].driver == driver){
			driverAll[o].lat = lat;
			driverAll[o].lon = lon;
		} else {
			number++;
		}
	};

	if(number = driverAll.length){
		driverAll.push(driverAdd);
	}

	res.jsonp({status:'ok'});
});



router.post('/driveranswer', function(req, res){
	var driver = req.body.driver;
	var passenger = req.body.passenger;

	for(var o in d_pAll){
		if(d_pAll[o].passenger == passenger){
			d_pAll[o].driver = driver;
			break;
		}
	}

	pushTerminal('da542abb1dfae4ad09104f6b', '332043472c2c2f0cea927e7d', driver, 'user');
	res.jsonp({status:'ok'});
});

router.post('/drivercancel', function(req, res){
	var driver = req.body.driver;

	var i = 0;
	for (var o in d_pAll){
		if(d_pAll[o].driver == driver){
			d_pAll.splice(i, 1);
			break;
		}
		i++;
	};

	pushTerminal('da542abb1dfae4ad09104f6b', '332043472c2c2f0cea927e7d', driver, 'user');
	res.jsonp({status:'ok'});
});

module.exports = router;






